<template>
  <div class="container">
    <div class="pay logout" @touchmove.prevent v-if="isPay">
      <div class="contents bg-f padding-24">
        <div class="close">
          <img :src="close" alt @click="toclose" />
        </div>
        <div class="top space-between">
          <p class="fs-36">需支付</p>
          <p class="color-r">
            &yen;
            <span>{{totalPrice}}</span>
          </p>
        </div>
        <p class="title color-9 fs-32">选择支付方式</p>
        <div class="pay-type">
          <div class="item space-between">
            <div class="left center-col">
              <img :src="alipay" alt />
              <p class="fs-32">支付宝支付</p>
            </div>
            <div class="right center" @click="apay">
              <img v-if="isAlipay" :src="checked" alt />
              <img v-else :src="check" alt />
            </div>
          </div>
          <div class="item space-between">
            <div class="left center-col">
              <img :src="wxpay" alt />
              <p class="fs-32">微信支付</p>
            </div>
            <div class="right center" @click="wpay">
              <img v-if="isWx" :src="checked" alt />
              <img v-else :src="check" alt />
            </div>
          </div>
        </div>
        <div class="pay-btn bg-p color-f fs-32 center" @click="paying">立即支付</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "pay-model",
  data() {
    return {
      title: "",
      isWx: false,
      isAlipay: false,
      wxpay: require("@/assets/index/wx-order.png"),
      alipay: require("@/assets/index/alipay.png"),
      check: require("@/assets/index/check-order.png"),
      checked: require("@/assets/index/checked-order.png"),
      close: require("@/assets/account/del.png"),
      isPay: false,
      orderNum: null //订单号
    };
  },
  props: {
    wantpay: Boolean,
    musicId: Number,
    memberId: Number,
    status: Number,
    totalPrice: Number
  },
  components: {},
  methods: {
    // 关闭模态框
    toclose() {
      this.isPay = false;
      this.$emit("fn", this.isPay);
    },
    apay() {
      this.isAlipay = !this.isAlipay;
      if (this.isAlipay) {
        this.isWx = false;
      }
    },
    wpay() {
      this.isWx = !this.isWx;
      if (this.isWx) {
        this.isAlipay = false;
      }
    },
    // 点击了立即支付
    paying() {
      if (this.isWx == false && this.isAlipay == false) {
        this.AlertWin("请选择支付方式！");
        return;
      }

      if (this.status == 1) {
        //   购买单曲
        const url = this.$host1 + `home/vip/MusicOrder`;
        var params = {
          music_id: this.musicId
        };
        this.$axios
          .post(url, params)
          .then(res => {
            if (res.data.code == 200) {
              // 微信网页支付
              this.orderNum = res.data.data.order_sn;
              if (this.isWx == true) {
                if (this.isapp == 0) {
                  this.saveOrder(this.orderNum);
                } else if (this.isapp) {
                  var sendData = {
                    type: "3",
                    data: this.orderNum
                  };
                  window.postMessage(JSON.stringify(sendData));
                }
              } else {
                if (this.isapp == 0) {
                  this.saveOrderAlipay(this.orderNum);
                } else if (this.isapp) {
                  var sendData = {
                    type: "8",
                    data: this.orderNum
                  };
                  window.postMessage(JSON.stringify(sendData));
                }
              }
            } else if (res.data.code == 0) {
              this.AlertWin(res.data.message);
            } else {
              this.AlertWin(res.data.message);
            }
          })
          .catch(err => {
            console.log(err, "err-----");
          });
      } else if (this.status == 2) {
        //购买音乐包
        const url = this.$host1 + `home/vip/vipOrder`;
        var params = {
          membership_time_id: this.memberId
        };
        this.$axios
          .post(url, params)
          .then(res => {
            if (res.data.code == 200) {
              // 微信网页支付
              this.orderNum = res.data.data.order_syn;
              console.log(this.orderNum,'--this.orderNum');
              if (this.isWx == true) {
                if (this.isapp == 0) {
                  this.saveMusicOrder(this.orderNum);
                } else if (this.isapp) {
                  var sendData = {
                    type: "3",
                    data: this.orderNum
                  };
                  window.postMessage(JSON.stringify(sendData));
                }
              } else {
                if (this.isapp == 0) {
                  this.saveMusicOrderAlipay(this.orderNum);
                } else if (this.isapp) {
                  var sendData = {
                    type: "8",
                    data: this.orderNum
                  };
                  window.postMessage(JSON.stringify(sendData));
                }
              }
            } else if (res.data.code == 0) {
              this.AlertWin(res.data.message);
            } else {
              this.AlertWin(res.data.message);
            }
          })
          .catch(err => {
            console.log(err, "err-----");
          });
      }
    },
    // 单曲购买---微信
    saveOrder(code) {
      const url = this.$host1 + `pay/music_index?order_no=` + code;
      window.location.href = url;
    },
    // 音乐包购买----微信
    saveMusicOrder(code) {
      const url = this.$host1 + `pay/vip_index?order_number=` + code;
      window.location.href = url;
    },
    // 音乐包购买----支付宝
    saveMusicOrderAlipay(code) {
      const url = this.$host1 + `ali_pay.php?order_sn=` + code;
      window.location.href = url;
    },
    // 单曲购买---支付宝
    saveOrderAlipay(code) {
      const url = this.$host1 + `ali_pay_music.php?order_sn=` + code;
      window.location.href = url;
    }
  },
  created() {
    this.isPay = this.wantpay;
    console.log(this.status, "---this.status");
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.container {
  .pay {
    .contents {
      width: 100%;
      height: 6.6rem;
      position: absolute;
      bottom: 0;
      left: 0;
      border-radius: 0.15rem 0.15rem 0 0;
      .close {
        width: 0.48rem;
        height: 0.48rem;
        position: absolute;
        margin-top: -0.8rem;
        right: 0.24rem;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .top {
        height: 1.8rem;
        border-bottom: 0.01rem solid #e9e9e9;
        .color-r {
          span {
            font-size: 0.6rem;
          }
        }
      }
      .title {
        font-weight: 500;
        color: rgba(68, 68, 68, 1);
        height: 1rem;
        line-height: 1rem;
      }
      .pay-type {
        .item {
          height: 1rem;
          .left {
            img {
              width: 0.44rem;
              height: 0.44rem;
              margin-right: 0.2rem;
            }
          }
          .right {
            width: 0.36rem;
            height: 0.36rem;
            img {
              width: 100%;
            }
          }
        }
      }
      .pay-btn {
        margin-top: 0.3rem;
        width: 100%;
        height: 0.9rem;
        border-radius: 0.45rem;
      }
    }
  }
}
</style>
